/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ServicesAccessData;

import DAO.*;
import POJO.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 *
 * @author ngochuy
 */
public class AdminImpl implements Admin.Iface {

    private TTransport tr;
    private Cassandra.Client client;

    public AdminImpl() {
        try {
            tr = new TFramedTransport(new TSocket(Common.Constants.HOST, Common.Constants.PORT));
            TProtocol proto = new TBinaryProtocol(tr);
            client = new Cassandra.Client(proto);
            tr.open();
            client.set_keyspace(Common.Constants.KEYSPACE);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean insertStatus(StatusPOJO status) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        boolean result = false;
        try {
            result = stt.insertStatus(status);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean updateContentStatus(String statusID, String content) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        boolean result = false;
        try {
            result = stt.updateContentStatus(statusID, content);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean updateTagsOnAStatus(String statusID, String tags) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        boolean result = false;
        try {
            result = stt.updateTagsOnAStatus(statusID, tags);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int updateTagsOnListStatus(List<String> listStatusID, String tags) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        int result = 0;
        try {
            result = stt.updateTagsOnListStatus(listStatusID, tags);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean updateIsPublicOnAStatus(String statusID, boolean state) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        boolean result = false;
        try {
            result = stt.updateIsPublicOnAStatus(statusID, state);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int updateIsPublicOnListStatus(List<String> listStatusID, boolean state) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        int result = 0;
        try {
            result = stt.updateIsPublicOnListStatus(listStatusID, state);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean deleteStatus(String statusID) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.deleteStatus(statusID);
    }

    @Override
    public int deleteListStatus(List<String> listStatusID) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.deleteListStatus(listStatusID);
    }

    @Override
    public StatusPOJO selectStatusWithID(String statusID) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.selectStatusWithID(statusID);
    }

    @Override
    public List<StatusPOJO> selectStatusFromTag(String tagName) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.selectStatusFromTag(tagName);
    }

    @Override
    public List<String> getListPaging(int limit) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.getListPaging(limit);
    }

    @Override
    public List<StatusPOJO> selectStatusFromPage(String startKey, int limit) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.selectStatusFromPage(startKey, limit);
    }

    @Override
    public List<String> getListPagingOnATag(String tagName, int limit) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.getListPagingOnATag(tagName, limit);
    }

    @Override
    public List<StatusPOJO> selectStatusFromPageOnATag(String tagName, String startKey, int limit) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        return stt.selectStatusFromPageOnATag(tagName, startKey, limit);
    }

    @Override
    public List<StatusPOJO> searchStatusByContent(String keyWord, String _start, String _end, String filter) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        List<StatusPOJO> result = null;
        try {
            result = stt.searchStatusByContent(keyWord, _start, _end, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public List<StatusPOJO> searchStatusByTag(String keyWord, String _start, String _end, String filter) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        List<StatusPOJO> result = null;
        try {
            result = stt.searchStatusByTag(keyWord, _start, _end, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public List<StatusPOJO> searchStatusByContentandTag(String keyWord, String tag, String _start, String _end, String filter) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        List<StatusPOJO> result = null;
        try {
            result = stt.searchStatusByContentandTag(keyWord, tag, _start, _end, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public TagPOJO selectTagWithTagName(String tagName) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        return tag.selectTagWithTagName(tagName);
    }

    @Override
    public List<TagPOJO> selectAllTag() throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        return tag.selectAllTag();
    }

    @Override
    public boolean updateIsPublicOnATag(String tagName, boolean state) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        boolean result = false;
        try {
            result = tag.updateIsPublicOnATag(tagName, state);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int updateIsPublicOnListTag(List<String> listTagNane, boolean state) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        int result = 0;
        try {
            result = tag.updateIsPublicOnListTag(listTagNane, state);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean insertStatusIntoTag(String tagName, String statusID) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        return tag.insertStatusIntoTag(tagName, statusID);
    }

    @Override
    public boolean removeStatusFromTag(String tagName, String statusID) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        return tag.removeStatusInATag(tagName, statusID);
    }

    @Override
    public boolean insertTag(String tagName, boolean state) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        boolean result = false;
        try {
            result = tag.insertTagInfo(tagName, state);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean deleteTag(String tagName) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        try {
            return tag.deleteTag(tagName);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    @Override
    public boolean deleteListTag(List<String> listTagName) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        try {
            return tag.deleteListTag(listTagName);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    @Override
    public List<TagPOJO> searchTags(String value, String _start, String _end, String filter) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        List<TagPOJO> result = null;
        try {
            result = tag.searchTags(value, _start, _end, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public UserPOJO selectInfoOfUser(String userID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.selectAllInfoOfAUser(userID);
    }

    @Override
    public boolean updateIsBannedOnAUser(String userID, boolean state) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.updateIsBannedOnAUser(userID, state);
    }

    @Override
    public int updateIsBannedOnListUser(List<String> listUserID, boolean state) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.updateIsBannedOnListUser(listUserID, state);
    }

    @Override
    public List<UserPOJO> selectAllUserBanned() throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.selectAllUserBanned();
    }

    @Override
    public CounterPOJO getCounter() throws TException {
        return CounterDAO.getCounter(client);
    }

    @Override
    public boolean isAdminUser(String userID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.isAdminUser(userID);
    }

    @Override
    public boolean updateIsAdminOnAUser(String userID, boolean state) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.updateIsAdminOnAUser(userID, state);
    }

    @Override
    public boolean updateIsAdminOnListUser(List<String> listUserID, boolean state) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.updateIsAdminOnListUser(listUserID, state);
    }

    @Override
    public List<UserPOJO> selectListAdminUser() throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<userLog> getuserLog() throws TException {
        userLogDAO user = userLogDAO.getInstance(client);
        List<userLog> result = null;
        try {
            result = user.getuserLog();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean setuserLog(String userID, String userName) throws TException {
        userLogDAO user = userLogDAO.getInstance(client);
        boolean result = false;
        try {
            result = user.setuserLog(userID, userName);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public List<favoriteLog> getfavoriteLog() throws TException {
        favoriteLogDAO fav = favoriteLogDAO.getInstance(client);
        List<favoriteLog> result = null;
        try {
            result = fav.getfavoriteLog();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean setfavoriteLog(String userID, String userName, String statusID) throws TException {
        favoriteLogDAO fav = favoriteLogDAO.getInstance(client);
        boolean result = false;
        try {
            result = fav.setfavoriteLog(userID, userName, statusID);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int getTotal_searchStatusByContent(String keyWord, String filter) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        int result = 0;
        try {
            result = stt.getTotal_searchStatusByContent(keyWord, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int getTotal_searchStatusByTag(String keyWord, String filter) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        int result = 0;
        try {
            result = stt.getTotal_searchStatusByTag(keyWord, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int getTotal_searchStatusByContentandTag(String keyWord, String tag, String filter) throws TException {
        StatusDAO stt = StatusDAO.getInstance(client);
        int result = 0;
        try {
            result = stt.getTotal_searchStatusByContentandTag(keyWord, tag, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public int getTotal_searchTags(String value, String filter) throws TException {
        TagDAO stt = TagDAO.getInstance(client);
        int result = 0;
        try {
            result = stt.getTotal_searchTags(value, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public List<StatusPOJO> selectAllStatus() throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        return status.selectAllStatus();
    }

    @Override
    public List<String> getListPagingUser(int limit) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.getListPagingUser(limit);
    }

    @Override
    public List<UserPOJO> selectListUserFromPage(String startKey, int limit) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.selectListUserFromPage(startKey, limit);
    }

    @Override
    public boolean All_cache() throws TException {
        try {
            //Khởi tạo cache.
            List<String> listMem = new ArrayList<String>();
            (StatusDAO.getInstance(client)).createCache(listMem);
            (TagDAO.getInstance(client)).createCacheTag(listMem);
            (UserDAO.getInstance(client)).createCache(listMem);
            Memcached.set("ListMemId", Common.Constants.EXPIRE, listMem);

        } catch (UnavailableException ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }

    @Override
    public boolean setUserAdmin(String userID, String passWord) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.setUserAdmin(userID, passWord);
    }

    @Override
    public boolean removeUserAdmin(String userID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.removeUserAdmin(userID);
    }

    @Override
    public UserAdminPOJO getUserAdmin(String userID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.getUserAdmin(userID);
    }

    @Override
    public List<UserAdminPOJO> getListUserAdminPOJO() throws TException {
        UserDAO user = UserDAO.getInstance(client);
        return user.getListUserAdminPOJO();
    }


}